//
//  BySideslipAnimator.m
//  Private_Object
//
//  Created by pro on 16/9/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "BySideslipAnimator.h"

@implementation BySideslipAnimator
{
    UIView *_fromTempView;
}
- (void)xw_setToAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIView *containerView = [transitionContext containerView];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    
    toVC.view.frame = CGRectOffset(containerView.bounds, -200, 0);
    [containerView addSubview:toVC.view];
    
    UIView *fromTempView = [fromVC.view snapshotViewAfterScreenUpdates:NO];
    fromTempView.frame = containerView.bounds;
    [containerView insertSubview:fromTempView aboveSubview:toVC.view];
    _fromTempView = fromTempView;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
    [_fromTempView addGestureRecognizer:tap];

    [UIView animateWithDuration:self.toDuration
                     animations:^{
                        
                         toVC.view.frame = containerView.bounds;
                         _fromTempView.frame = CGRectOffset(containerView.bounds, SCREEN_WIDTH - 100, 0);
                     }
                     completion:^(BOOL finished) {
                         
                         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                     }];
}
- (void)back {
    if (self.backConfig) {
        self.backConfig();
    } else {
        NSLog(@"请设置返回回调，否则不能点击返回");
    }
}

- (void)xw_setBackAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    UIView *containerView = [transitionContext containerView];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    
    toVC.view.frame = _fromTempView.frame;
    [_fromTempView removeFromSuperview];
    [containerView addSubview:toVC.view];

    fromVC.view.frame = containerView.bounds;
    
    [UIView animateWithDuration:self.backDuration
                     animations:^{
                         
                         
                         toVC.view.frame = containerView.bounds;
                         fromVC.view.frame = CGRectOffset(containerView.bounds, -100, 0);
                     }
                     completion:^(BOOL finished) {
                         
                         
                         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                     }];
}

@end
